<?php

class WeixinScript extends ScriptBase
{
	private $mongo = null;
	private $mysql = null;
	private $readCollection = null;
	private $scrapy = null;

	public function __construct()
	{
		$this->mongo = new MongoClient("mongodb://newbee:9^4%NbAdwS@115.28.18.40:27017");
		$this->mysql = n_api('mysql')->cname('weixin');
		$this->readCollection = $this->mongo->selectDB('weixin')->selectCollection('read');
		$this->articleCollection = $this->mongo->selectDB('weixin')->selectCollection('article');
		$this->weixinCollection = $this->mongo->selectDB('weixin')->selectCollection('mpListFina');	
		$this->scrapy = n_api('scrapy');
	}

	public function articleCount()
	{
		$query = "select count(*) as sum_all from article_details";
		$rs = $this->mysql->fetchFirst($query);
		$sum_all = $rs['sum_all'];

		$query = "select sum(read_num) as sum_read, sum(like_num) as sum_like from article_day";
		$rs = $this->mysql->fetchFirst($query);
		$sum_read = $rs['sum_read'];
		$sum_like = $rs['sum_like'];

		$data = [
			'article_total' => $sum_all,
			'read_total' => $sum_read,
			'like_total' => $sum_like,
			'ctime' => time()
		];

		$this->mysql->insert('article_all', $data);
	}

	public function test() {
		$ch = curl_init('https://mp.weixin.qq.com/mp/getappmsgext?__biz=MjM5MjcxMjM2Mw==&mid=400334413&sn=45d88e79df623e728ff197deaeeba499&idx=2&scene=4&devicetype=android-21&version=&f=json&is_need_ad=1&comment_id=0&is_need_reward=0&both_ad=1&reward_uin_count=0&uin=MTA4NzA3MDk3MQ==&key=04dce534b3b035efee7871f950a73c60592eee003edaac4dc4cebd27d46a5dcb6dd01e240c7d27ab722cd98255a1a21d&pass_ticket=rYPTJNcz01Lq4qWJoJA91ma6gbvAziS/4/OKBQWCuRGBm6brUO6ybjPNCqp3udMn&x5=1');
		// $ch = curl_init('http://mp.weixin.qq.com/mp/getappmsgext?__biz=MjM5MjcxMjM2Mw==&mid=400334413&sn=45d88e79df623e728ff197deaeeba499&idx=2&scene=4&devicetype=android-21&version=&f=json&is_need_ad=1&comment_id=0&is_need_reward=0&both_ad=1&reward_uin_count=0&uin=MTEyNDIwNTY2OQ==&key=04dce534b3b035ef7737dc64660e52b935347d06eb31b0f577c2840840668094bc17ab46b4740fd38fea92cd4f58f08c&pass_ticket=K1d4J7Kj/cQhHtUjaMKk0uoSoDKmcG1oY9dcS0EKe7cmsdVrPujaLXx+Iyeg+zIH&x5=1');
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($ch, CURLOPT_HEADER, 1);
		curl_setopt($ch, CURLOPT_USERAGENT, 'User-Agent: Mozilla/5.0 (Linux; U; Android 5.0.2; zh-cn; MI 2S Build/LRX22G) AppleWebKit/533.1 (KHTML, like Gecko)Version/4.0 MQQBrowser/5.4 TBS/025469 Mobile Safari/533.1 MicroMessenger/6.2.5.51_rfe7d7c5.621 NetType/WIFI Language/zh_CN');
		curl_setopt($ch, CURLOPT_HTTPHEADER, [
			'Accept: */*',
			'Accept-Charset: utf-8, iso-8859-1, utf-16, *;q=0.7',
			'Accept-Language: zh-CN',
			'Connection: keep-alive',
			'X-Requested-With: XMLHttpRequest'
		]);
		curl_setopt($ch, CURL_POST, 1);
		curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query([]));

		$rs = curl_exec($ch);
		var_dump($rs);
	}

	public function remove() {
		$skip = 0;
		$limit = 100;

		while (true) {
			$query = "select article_mongoid from article_details where article_ptime >= 1446825600 and article_ptime < 1446912000 limit $skip, $limit";
			$rs = $this->mysql->select($query);
			
			if (count($rs) == 0)
				exit;
			
			foreach ($rs as $value) {
				$imgUrl = '/opt/webapps/fina/static/weixin_thumbnail/' . $value['article_mongoid']. '.jpg';
				@unlink($imgUrl);
				echo $imgUrl . " deleteed\n";
			}

			$skip += $limit;
		}
	}

	public function delImg() {
		$stime = 1451232000;
		$basePath = "/opt/webapps/fina/static/weixin_thumbnail/";
		$rs = $this->mysql->select("select id, article_mongoid, article_ptime from article_details where article_ptime < $stime");
		foreach ($rs as $value) {
			$id = $value['id'];
			$mongoid = $value['article_mongoid'];
			$filePath = $basePath . $mongoid . ".jpg";
			if (file_exists($filePath)) {
				unlink($filePath);
			}
			$this->mysql->exec("delete from article_details where id = $id");
			echo $id . " deleted\n";
		}
	}
}